﻿#if NETSTANDARD2_0
using System;

namespace Newtonsoft.Json.Converters
{
    /// <summary>
    /// 转换 DateTimeOffset 类型数据为自定义格式的 Json 字符串
    /// </summary>
    public class CustomerDateTimeOffsetConverter : IsoDateTimeConverter
    {
        /// <summary>
        /// 默认构造函数 字符串默认为 "yyyy-MM-dd HH:mm:ss"
        /// </summary>
        public CustomerDateTimeOffsetConverter() : this("yyyy-MM-dd HH:mm:ss")
        {

        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CustomerDateTimeOffsetConverter(string format)
        {
            DateTimeFormat = format;
        }

        /// <summary>
        /// 读取方法
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="objectType"></param>
        /// <param name="existingValue"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return base.ReadJson(reader, objectType, existingValue, serializer);
        }

        /// <summary>
        /// 写入方法
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="value"></param>
        /// <param name="serializer"></param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value is DateTime v1 && v1 == DateTime.MinValue ||
                value is DateTimeOffset v2 && v2 == DateTimeOffset.MinValue) writer.WriteValue((object)null);
            else base.WriteJson(writer, value, serializer);
        }
    }
}
#endif

#if NETCOREAPP3_0
using System.Buffers;
using System.Buffers.Text;

namespace System.Text.Json.Serialization
{
    /// <summary>
    /// 转换 DateTime 类型数据为自定义格式的 Json 字符串
    /// </summary>
    public class CustomerDateTimeOffsetConverter : JsonConverter<DateTimeOffset>
    {
        /// <summary>
        /// 获得/设置 DateTime 类型数据自定义字符串 默认 "yyyy-MM-dd HH:mm:ss"
        /// </summary>
        public string DateTimeFormat { get; set; } = "yyyy-MM-dd HH:mm:ss";

        /// <summary>
        /// 默认构造函数 字符串默认为 "yyyy-MM-dd HH:mm:ss"
        /// </summary>
        public CustomerDateTimeOffsetConverter() : this("yyyy-MM-dd HH:mm:ss")
        {

        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public CustomerDateTimeOffsetConverter(string format)
        {
            DateTimeFormat = format;
        }

        /// <summary>
        /// 读取方法
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="typeToConvert"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.String)
            {
                var span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
                if (Utf8Parser.TryParse(span, out DateTimeOffset dtm, out int bytesConsumed) && span.Length == bytesConsumed)
                    return dtm;

                if (DateTimeOffset.TryParse(reader.GetString(), out dtm)) return dtm;
            }
            return reader.GetDateTimeOffset();
        }

        /// <summary>
        /// 写入方法
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="value"></param>
        /// <param name="options"></param>
        public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString(DateTimeFormat));
        }
    }
}
#endif
